package com.hnevc.xiaoming.httpactivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.httpbean.RePassWordBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;

public class MyVolleyRegistered extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView title_view;
    private EditText et_name_zc;
    private EditText et_password_zc;
    private EditText et_password_aszc;
    private Button btn_ok_zc;
    private String name;
    private String zc;
    private String aszc;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_registered);
        initView();


    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        title_view = (TextView) findViewById(R.id.title_view);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("注冊界面");

        et_name_zc = (EditText) findViewById(R.id.et_name_zc);
        et_password_zc = (EditText) findViewById(R.id.et_password_zc);
        et_password_aszc = (EditText) findViewById(R.id.et_password_aszc);
        btn_ok_zc = (Button) findViewById(R.id.btn_ok_zc);

        btn_ok_zc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok_zc:
                if (submit()==true) {
                    //网络地址
                    String url = "http://anli-wechat.boruiman.com/api/UserServer/user_register";

                    //body傳遞給服務器的參數
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("username", name);
                        jsonObject.put("password", zc);
                        jsonObject.put("repassword", aszc);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    //创建队列
                    RequestQueue requestQueue = Volley.newRequestQueue(MyVolleyRegistered.this);
                    //请求主体，1请求方式，2请求地址，3请求参数，4 5 分别是成功与失败响应监听
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
//                            Gson gson = new Gson();
//                            //将响应成功的json数据映射到实体类当中；
//                            RePassWordBean rePassWordBean = gson.fromJson(jsonObject.toString(), RePassWordBean.class);
                            Toast.makeText(MyVolleyRegistered.this, "" + jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(MyVolleyRegistered.this, "网络请求失败！", Toast.LENGTH_SHORT).show();
                        }
                    });

                    //加入网络队列
                    requestQueue.add(jsonObjectRequest);
                }
                break;
        }
    }


    private boolean submit() {
        // validate
        name = et_name_zc.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        zc = et_password_zc.getText().toString().trim();
        if (TextUtils.isEmpty(zc)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        aszc = et_password_aszc.getText().toString().trim();
        if (TextUtils.isEmpty(aszc)) {
            Toast.makeText(this, "aspassword不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
            return true;
    }
}
